from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from .models import UserInfo
from .models import NewsInfo
from .models import TypeInfo
import time

# Create your views here.
def news(request, id):
    thisarticle = list(NewsInfo.objects.filter(id=id).values("id", "NewsTitle", "NewsAuthor", "NewsContent","NewsUpdateTime"))[0]
    return render(request, "detail.html", {"thisarticle": thisarticle, })

def register(request):
    if(request.session.has_key("name123456789")):
        return HttpResponseRedirect("/")
    if(request.method == "POST"):
        UserName = request.POST["userid"]
        UserPassword = request.POST["userpwd"]
        UserPhone = request.POST["phone"]
        UserEmail = request.POST["email"]
        UserSex = request.POST["sex"]
        UserAge = request.POST["age"]
        UserInfo.objects.create(UserName=UserName,UserPassword=UserPassword,UserPhone=UserPhone,UserEmail=UserEmail,UserSex=UserSex,UserAge=UserAge,UserLevel=0)
        time.sleep(1)
        request.session["name123456789"] = UserName
        nav1 = UserName
        nav2 = "/"
        nav3 = "退出"
        nav4 = "/logout"
        return render(request,"index.html",{"nav1":nav1,"nav2":nav2,"nav3":nav3,"nav4":nav4})
    return render(request,"register.html")

def login(request):
    if(request.session.has_key("name123456789")):
        return HttpResponseRedirect("/")
    if(request.method == "POST"):
        username = request.POST["user"]
        userpassword = request.POST["pwd"]
        islogin = UserInfo.objects.filter(UserName=username,UserPassword=userpassword)
        if(islogin):
            request.session["name123456789"] = username
            return HttpResponseRedirect("/")
        else:
            return HttpResponse("登录失败")
    return render(request,"login.html")

def index(request):
    if(request.session.has_key("name123456789")):
        nav1 = request.session["name123456789"]
        nav2 = "/"
        nav3 = "退出"
        nav4 = "/logout"
    else:
        nav1 = "注册"
        nav2 = "/register"
        nav3 = "登录"
        nav4 = "/login"
    #return render(request,"index.html",{"nav1":nav1,"nav2":nav2,"nav3":nav3,"nav4":nav4})
    return render(request,"index.html")

def logout(request):
    del request.session["name123456789"]
    return HttpResponseRedirect("/")

def newslist(request):
    if (request.session.has_key("name123456789")):
        nav1 = request.session["name123456789"]
        nav2 = "/"
        nav3 = "退出"
        nav4 = "/logout"
    else:
        nav1 = "注册"
        nav2 = "/register"
        nav3 = "登录"
        nav4 = "/login"
        # return render(request,"list.html",{"nav1":nav1,"nav2":nav2,"nav3":nav3,"nav4":nav4})
    return render(request, "list.html")

def publish(request):
    if(request.session.has_key("name123456789") == False):
        return HttpResponseRedirect("/login")
    else:
        nav1 = request.session["name123456789"]
        nav2 = "/"
        nav3 = "退出"
        nav4 = "/logout"

    #get typeid and typename for render input
    TypeAll = TypeInfo.objects.values("id","TypeName")
    TypeId = []
    TypeName = []
    for item in TypeAll:
        TypeId.append(item["id"])
        TypeName.append(item["TypeName"])
    TypeArray = zip(TypeId,TypeName)

    Users = list(UserInfo.objects.filter(UserName=request.session["name123456789"]).values())[0]
    UserId = Users["id"]
    UserName = Users["UserName"]
    UserLevel = Users["UserLevel"]
    if(UserLevel != 2):
        return HttpResponseRedirect("/")
    if(request.method == "POST"):
        Content = request.POST[""]
        thisTypeId = request.POST[""]
        Title = request.POST[""]
        Digest = request.POST[""]
        UpdateTime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
        NewsInfo.objects.create(NewsTitle=Title,NewsDigest=Digest,NewsContent=Content,NewsAuthor=UserName,NewsUserID=UserId,NewsUpdateTime=UpdateTime,NewsTypeID=thisTypeId)
        return HttpResponseRedirect("/")
    return render(request,"publish.html",{"TypeArray":TypeArray,"nav1":nav1,"nav2":nav2,"nav3":nav3,"nav4":nav4})

def help(request):
    return HttpResponse("这是帮助")

